﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Office.Interop.Excel;
using System.Text;

namespace ExcelJsonParser
{
    class JsonWorkSheet : JsonWorkSheetBase
    {

        //private Worksheet _worksheet;
        private object _jsonSource;
        private JsonWorkSheetTable _jsonWorkSheetTable;
        public JsonWorkSheetTable JsonWorkSheetTable{ get{return _jsonWorkSheetTable;}}
        public JsonWorkSheet(Worksheet ws):base(ws)
        {
            _worksheet = ws;
            if (ws.ListObjects.Count > 0)
            {
                _jsonWorkSheetTable = new JsonWorkSheetTable(ws.ListObjects.Item[1]);    
            }
        }

        private void SetWorkSheetJson(object jsObject)
        {
        }
        private object GenerateJson()
        {
            return null;
        }

        public void SetJsonSource(object jsonObject)
        {
            _jsonSource = jsonObject;
            _worksheet.Select();
            RefreshFromJsonSource();
        }

        private void RefreshFromJsonSource()
        {
            Range startCell = _worksheet.Columns[1].Rows[1];
            _jsonWorkSheetTable = new JsonWorkSheetTable(startCell, _jsonSource);
            ((Workbook)_worksheet.Parent).Windows[1].Visible = true;
            _worksheet.Activate();
        }

        internal object ExportJson()
        {
            return JsonWorkSheetTable.GetOutputJson(_worksheet);
        }
    }
}
